package com.sdry.web.controller.cb;

import com.sdry.model.lz.Customer;
import com.sdry.model.tdd.emailRecord.EmailRecord;
import com.sdry.service.cb.ReportFormService;
import com.sdry.service.jyy.UpService;
import com.sdry.service.tdd.emailRecord.EmailRecordService;
import com.sdry.utils.EmailUtils;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class RemindUserJob implements Job {
	

    @Autowired
    EmailRecordService emailRecordService;
    @Autowired
    UpService upService;

    public void execute(JobExecutionContext context) throws JobExecutionException{

        System.out.println("----我执行了发送邮件定时，时间是---" + new Date());
        EmailRecord emailRecord = new EmailRecord();
        emailRecord.setRemark("0");
        List<EmailRecord> emailRecordList = emailRecordService.queryAllByMution(emailRecord);
        int num = sendQQMailNumController.sendQQMail_num;
        if (emailRecordList.size() > num) {
        	//所有邮件接收人
        	Map<Long, List<EmailRecord>> map = emailRecordList.stream().collect(Collectors.groupingBy(EmailRecord::getReceivePeopleId));
        	//遍历集合
        	for(Map.Entry<Long, List<EmailRecord>> entry : map.entrySet()){
        		Long key = entry.getKey();
	            String emailContent = "";
        		List<EmailRecord> list=(List<EmailRecord>)entry.getValue();
	            for (EmailRecord emailRecord2 : list) {
	            	emailContent = emailContent + emailRecord2.getEmailContent()+";";
				}
	            Customer customer = upService.queryCustomerEmailById(key);
	            try {
                    //邮件发送
                    EmailUtils.sendQQMail(customer.getCustomer_email(), "库存报警", emailContent);
                    for (EmailRecord emailRecord2 : list) {
	                    //修改状态为已发送
	                    emailRecord2.setRemark("1");
	                    emailRecordService.update(emailRecord2);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        	}
        	/*for (EmailRecord emailRecord2 : emailRecordList) {
                Customer customer = upService.queryCustomerEmailById(emailRecord2.getReceivePeopleId());
                try {
                    //邮件发送
                    EmailUtils.sendQQMail(customer.getCustomer_email(), "库存报警", emailRecord2.getEmailContent());
                    //修改状态为已发送
                    emailRecord2.setRemark("1");
                    emailRecordService.update(emailRecord2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
        }
    }


}
